package com.xunk.scene.enums;

import com.xunk.common.core.interfaces.EnumValidateInterface;
import com.xunk.common.core.utils.StringUtils;
import lombok.Getter;

import java.util.Objects;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.scene.enums
 * @ClassName: RepeatTypeEnum
 * @Author: luanzhiwei
 * @Description: 重复类型枚举
 * @Date: 2021/5/12 15:10
 * @Version: 1.0
 */
@Getter
public enum RepeatTypeEnum implements EnumValidateInterface {


    DAY("DAY", "每天"),
    WORKDATE("WORKDATE", "每周一到周五"),
    WEEKEND("WEEKEND", "每周末"),
    ALLDAY("", "全天"),
    //每周几
    WEEK("WEEK", "每周");


    RepeatTypeEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    /**
     * 码值
     */
    private String code;
    /**
     * 描述
     */
    private String desc;

    /**
     * 根据code获取枚举
     *
     * @param code
     * @return
     */
    public static RepeatTypeEnum getByCode(String code) {
        for (RepeatTypeEnum value : RepeatTypeEnum.values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 判断是否是可用枚举
     *
     * @param code
     * @return
     */
    @Override
    public Boolean isValidEnum(String code) {
        for (RepeatTypeEnum value : RepeatTypeEnum.values()) {
            if (value.getCode().equals(code)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 拼接描述
     *
     * @param code
     * @param specificDay
     * @return
     */
    public static String getDescription(String code, String specificDay) {
        StringBuffer stringBuffer = new StringBuffer();
        final RepeatTypeEnum byCode = getByCode(code);
        if (!Objects.isNull(byCode) && StringUtils.isNotEmpty(byCode.getCode())) {
            stringBuffer.append(byCode.getDesc());
        }
        if (byCode.getCode().equals(RepeatTypeEnum.WEEK.getCode())) {
            stringBuffer.append(specificDay);
        }
        return stringBuffer.toString();
    }
}
